package com.example.activiti.controller.login;

import com.example.activiti.common.MessageBody;
import com.example.activiti.config.UserLocal;
import com.example.activiti.model.ActUser;
import com.example.activiti.model.LoginDTO;
import com.example.activiti.service.ActUserService;
import com.example.activiti.utils.JwtTokenUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Mr.yuan
 * @version v1.0
 * @date 2019/11/11 19:48
 **/
@RestController
@RequestMapping("/login")
public class LoginController {

    private final ActUserService actUserService;

    public LoginController(ActUserService actUserService) {
        this.actUserService = actUserService;
    }

    @RequestMapping(value = "/user/login")
    public MessageBody login(@RequestBody LoginDTO loginDTO){

        ActUser actUser = actUserService.findUser(loginDTO);
        String token = JwtTokenUtils.createJwt(actUser);
        return MessageBody.success(token);
    }

    @RequestMapping(value = "/user/getUserInfo")
    public MessageBody userInfo(){

        String id = UserLocal.getUser().getId();
        ActUser actUser = actUserService.selectByPrimaryKey(id);

        return MessageBody.success(actUser);
    }

    @RequestMapping(value = "/user/logout")
    public MessageBody logout(){
        JwtTokenUtils.EXPIRE_TOKEN.add(UserLocal.getUser().getToken());
        return MessageBody.success();
    }

}
